package com.example.mobile2021606145zbq0511;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

    String urlString = "http://192.168.29.80:3000/posts";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tvMainHttp = findViewById(R.id.tvMainHttp);

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url = new URL(urlString);
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    InputStream inputStream = connection.getInputStream();
                    InputStreamReader isr = new InputStreamReader(inputStream);//字符流
                    BufferedReader br = new BufferedReader(isr);//字符缓冲流
                    String temp=br.readLine();
                    Log.i("wdnmd","run"+temp);


                    br.close();
                    isr.close();
                    inputStream.close();
                    connection.disconnect();

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        }.start();






    }
}